// pass: 66.67%
function maxMoney(root) {
    let res = 0;

    let isLast = function (root) {
        return root.left === null && root.right === null;
    }

    let helper = function (root) {
        if (!root) return 0;

        let leftNode = root.left;
        let rightNode = root.right;
        if (leftNode !== null && rightNode !== null) {
            if (isLast(leftNode) && isLast(rightNode)) return 5;
            else if (isLast(leftNode) && !isLast(rightNode)) return 2 + helper(rightNode);
            else if (!isLast(leftNode) && isLast(rightNode)) return 2 + helper(leftNode);
            else return helper(leftNode) + helper(rightNode);
        } else if (leftNode !== null && rightNode === null) {
            if (isLast(leftNode)) return 2;
            else return 2 + helper(root.leftNode)
        } else if (rightNode !== null && leftNode === null) {
            if (isLast(rightNode)) return 2;
            else return 2 + helper(root.rightNode)
        }
    }

    res = helper(root);
    return res;
}